package com.example.firstspringboot.controller;

import com.example.firstspringboot.bean.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>Thymeleaf Test</p>
 */

@Controller
@RequestMapping("/th")
public class ThController {
    /**
     * <p>展示<code>"resources/templates/th/test1.html"</code>视图</p>
     *
     * @param model thymeleaf所用参数
     * @return 视图文件路径
     */
    @RequestMapping("/test1")
    public String test1(Model model) {
        Student student = new Student(2021001, "小明");
        model.addAttribute("stu", student);
        model.addAttribute("count", 666);
        return "th/test1";
    }

    @RequestMapping("/test2")
    public String test2(Model model) {
        Student student = new Student(2021001, "小明");
        model.addAttribute("stu", student);
        model.addAttribute("site", "武汉科技大学");
        model.addAttribute("url", "https://www.wust.edu.cn");
        model.addAttribute("imgURL", "/images/苏轼.jpg");
        return "th/test2";
    }

    /**
     * <p>thymeleaf <code>th:each</code> 示例1</p>
     */
    @RequestMapping("/test3")
    public String test3(Model model) {
        List<Student> list = new ArrayList<Student>();
        list.add(new Student(2021001, "小明"));
        list.add(new Student(2021002, "小丽"));
        list.add(new Student(2021003, "小王"));
        model.addAttribute("list", list);

        return "th/test3";
    }

    /**
     * <p>thymeleaf <code>th:each</code> 示例2</p>
     */
    @RequestMapping("/test4")
    public String test4(Model model) {
        List<Student> list = new ArrayList<Student>();
        list.add(new Student(2021001, "小明"));
        list.add(new Student(2021002, "小丽"));
        list.add(new Student(2021003, "小王"));
        model.addAttribute("list", list);

        return "th/test4";
    }

    /**
     * <p>thymeleaf form 表单示例</p>
     */
    @RequestMapping("/test5")
    public String test5(Model model) {
        Student student = new Student(2020001, "小明");
        model.addAttribute("stu", student);
        return "th/test5";
    }

    /**
     * <p>thymeleaf form 表单提交用</p>
     * <p>也可以按照下面方法写</p>
     * <pre>
     *     public Student test6(Student student) {
     *         return student;
     *     }
     * </pre>
     */
    @RequestMapping("/test6")
    @ResponseBody
    public Student test6(Integer id, String name) {
        Student student = new Student(id, name);
        return student;
    }

    static List<Student> stuList = new ArrayList<Student>();

    static {
        stuList.add(new Student(2021001, "小明"));
        stuList.add(new Student(2021002, "小丽"));
        stuList.add(new Student(2021003, "小王"));
    }

    @GetMapping("/")
    public String getAllStudents(Model model) {
        model.addAttribute("list", stuList);
        return "th/index";
    }

    @GetMapping("/edit/{id}")
    public String edit(Model model, @PathVariable("id") Integer id) {
        Student student = findById(id);
        model.addAttribute("stu", student);
        return "th/edit";
    }

    // url: localhost:8080/th/save  提交值为id和name post方式
    @PostMapping("/save")
    public String save(Integer id, String name) {
        Student student = findById(id);
        if (student != null) student.setName(name);
        return "redirect:/th/";
    }

    // url: localhost:8080/th/delete/2020001
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        Student student = findById(id);
        stuList.remove(student);
        return "redirect:/th/";     // redirect:表示重定向
    }

    @GetMapping("/create")
    public String create() {
        return "th/create";
    }

    @PostMapping("/saveForCreate")
    public String saveForCreate(Integer id, String name) {
        Student student = findById(id);
        if (student == null) {
            stuList.add(new Student(id, name));
        }
        return "redirect:/th/";
    }

    // 内部私有方法，外部不能访问
    private Student findById(Integer id) {
        for (Student s : stuList) {
            if (s.getId().equals(id)) {
                return s;
            }
        }
        return null;
    }
}
